<?php

namespace app\common\model;

use think\facade\Cache;
use think\Model;

class WhiteIpModel extends Model
{
    const WHITE_IP_INDEX_KEY = "white_ip";

    protected static function init()
    {
        WhiteIpModel::afterInsert(function ($user) {
            Cache::rm(static::WHITE_IP_INDEX_KEY);
        });
        WhiteIpModel::afterUpdate(function ($user) {
            Cache::rm(static::WHITE_IP_INDEX_KEY);
        });
        WhiteIpModel::afterDelete(function ($user) {
            Cache::rm(static::WHITE_IP_INDEX_KEY);
        });
    }

    const status = [
        0 => '失效',
        1 => "生效"
    ];

    public function getStatusAttr($v)
    {
        return self::status[$v];
    }

    public static function lists()
    {
        return Cache::remember(static::WHITE_IP_INDEX_KEY, function () {
            $list = static::where('status', 1)->select()->toArray();
            return array_column($list, 'ip');
        });
    }
}
